<?php


namespace App\Http\Server;


use App\Models\Goods;
use App\Models\GoodsPrice;

class GoodsServer
{

    public function addGoodsServer($id,$goods_name,$manufacturer_id,$inventory,$cost_price,$sales_price){
        if(isset($id) && !empty($id)){
            $model = Goods::find($id);
        }else{
            $model = new Goods();
        }
        $model->goods_name = $goods_name;
        $model->manufacturer_id = $manufacturer_id;
        $model->inventory = $inventory;
        $model->cost_price = $cost_price;
        $model->sales_price = $sales_price;
        $model->profit = $sales_price-$cost_price;
        $model->save();
        GoodsPrice::insert([
            'goods_id'=>$model->goods_id,
            'cost_price'=>$cost_price,
            'sales_price'=>$sales_price,
            'now_date'=>date("Y-m-d",time())
        ]);
        return ['status'=>200,'data'=>$model,'msg'=>'success'];
    }


    public function getGoodsListServer($params,$per_page=10)
    {
        // 执行查询
        $goods = Goods::Search($params)->with('manufacturer')->paginate($per_page);
        return ['status'=>200,'data'=>$goods,'msg'=>'success'];
    }
}
